﻿	package app.demo.MyTouchApp{
	import flash.events.TUIO; // allows to connect to touchlib/tbeta
 
    import flash.display.*;
    import flash.events.*;
	import flash.events.MouseEvent;
    import flash.geom.*; // needed for curPt;
	import flash.display.SimpleButton;
	import flash.net.URLRequest;
	import flash.display.FrameLabel;
	import fl.controls.Label;
	import fl.controls.Button;
	import fl.controls.*;
	import fl.core.*;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.system.fscommand;
	import flash.system.*;
	import fl.transitions.easing.*;
	import flash.media.*;
	
	public class wellDoneOnePlayer extends MovieClip {
		private var color:uint = 0xff0000; // color of lines
        private var thickness:Number = 30; // thickness of lines
        private var blobs:Array = new Array(); // blobs we are currently interacting with
        private var lines:Array = new Array(); // array to hold the lines
		private var numButtonWidth:Number = 100;
		private var numButtonHeight:Number = 50;
		
		private var imagesArray:Array = ["darkBackground.png","welldone.png","cup.png"];
		var imageBackground:Loader = new Loader();
		public var buttonPlay:Button;
		public var buttonMainMenu:Button;
		public var buttonExit:Button;
		var topicLabel:Label = new Label();
		var chooseLabel:Label = new Label();
		var myMaster:master;
		var movie1:MovieClip = new MovieClip();
		var topic:Loader = new Loader();
		var cup:Loader  = new Loader();
		var snd:Sound;
		
		public function wellDoneOnePlayer(m:master):void {
			
			//fscommand("allowscale", "false");
			//fscommand("fullscreen", "false");
			//Stage.showMenu = false;
			
			
			snd= new Sound();
			snd.load(new URLRequest("welldone.mp3"));
			snd.play();

			
			myMaster=m;
			//--------connect to TUIO-----------------
			TUIO.init(this,'localhost',3000,'',true);
            trace("MyPage Initialized");
			
			addChild(movie1);
			
			
			//addEventListener(TouchEvent.MOUSE_DOWN, downEvent, false, 0, true); // run when finger down
            addEventListener(TouchEvent.MOUSE_UP, upEvent, false, 0, true); // run when finger up
            addEventListener(Event.ENTER_FRAME, updater, false, 0, true); // keep running
			
			//////////////////
			//Background:
			imageBackground.load(new URLRequest(imagesArray[0]));
			imageBackground.scaleX=1.4;
			movie1.addChild(imageBackground);
			
			
			cup.load(new URLRequest(imagesArray[2]));
			//imageBackground.scaleX=1.4;
			cup.x=420;
			cup.y=220;
			movie1.addChild(cup);
			
			//Play Button:
			buttonPlay = new Button();
			buttonPlay.label="PLAY AGIAN";
			buttonPlay.width=200;
			buttonPlay.height=40;
			buttonPlay.x=400;
			buttonPlay.y=510;
			//buttonPlay.emphasized=true;
			buttonPlay.addEventListener(TouchEvent.MOUSE_DOWN,Again);
			movie1.addChild(buttonPlay);
			
			//Main Menu button:
			buttonMainMenu = new Button;
			buttonMainMenu.x=400;
			buttonMainMenu.y=580;
			buttonMainMenu.width=200;
			buttonMainMenu.height=40;
			buttonMainMenu.label="MAIN MENU";
			buttonMainMenu.addEventListener(TouchEvent.MOUSE_DOWN,goMain);
			movie1.addChild(buttonMainMenu);
			
			//Exit Button:
			buttonExit = new Button;
			buttonExit.x=400;
			buttonExit.y=650;
			buttonExit.width=200;
			buttonExit.height=40;
			buttonExit.label="EXIT";
			buttonExit.addEventListener(TouchEvent.MOUSE_DOWN,closeApp);
			movie1.addChild(buttonExit);
			
			//Page Title (TOPIC MATCHING):
			var myTextFormat1:TextFormat = new TextFormat();
			myTextFormat1.font = "Kristen ITC";
			myTextFormat1.size = 70;
			
			topicLabel.text = "WELL DONE!!";
			topicLabel.width= 600;
			topicLabel.height= 300;
			topicLabel.x=260;
			topicLabel.visible=true;
			topicLabel.setStyle("textFormat", myTextFormat1);
			//movie1.addChild(topicLabel);
			
			topic.load(new URLRequest(imagesArray[1]));
			topic.x=70;
			topic.y=-30;
			topic.scaleX=1.1;
			topic.scaleY=1.1;
			movie1.addChild(topic);
			

		}
		
		function closeApp(event:TouchEvent):void
		{
     		 fscommand("quit");    
     	}
		private function goMain(e:TouchEvent):void
		{
			myMaster.goBackToMain1();
		}
		
		private function Again(e:TouchEvent):void
		{
			myMaster.playAgain1();
		}
		
		function updater(e:Event):void {
            for (var i:int = 0; i < blobs.length; i++) { 
                var tuioobj:TUIOObject=TUIO.getObjectById(blobs[i].id);
 
                // if not found, then it must have died..
                if (! tuioobj) {
                    removeBlob(blobs[i].id);
                } else if (parent != null) {
 
                    lines["line"+blobs[i].id].graphics.lineTo(tuioobj.x, tuioobj.y);
                }
            }
        }
		
		
		// when touched, this calls addblob with the blob ID and the X/Y of where it started
        public function downEvent(e:TouchEvent):void {
            var curPt:Point=parent.globalToLocal(new Point(e.stageX,e.stageY));
            addBlob(e.ID, curPt.x, curPt.y);
            e.stopPropagation();
        }
		
		function clickHandler(evtObj:MouseEvent){
		trace("The " + evtObj.target.label + " button was clicked");
		}
 
        // When a finger stops touching, call removeBlob
        public function upEvent(e:TouchEvent):void {
            removeBlob(e.ID);
            e.stopPropagation();
        }
		
		function addBlob(id:Number, origX:Number, origY:Number):void {
            blobs.push( {id: id, origX: origX, origY: origY, myOrigX: x, myOrigY:y} );
 
            var line:Sprite = new Sprite();
            lines["line"+id] = line;
            lines["line"+id].graphics.lineStyle(thickness, color); //<-- config above
            lines["line"+id].graphics.moveTo(origX, origY); //starting point of line to be drawn
            addChild(lines["line"+id]); // add line to stage
        }
 
        // when called, this function removes the blobs from the array
        // cycles through the blobs array until the blob is found by its ID, then it is removed.
        function removeBlob(id:Number):void {
            for (var i=0; i < blobs.length; i++) {
                if (blobs[i].id==id) {
                    blobs.splice(i, 1);
                    return;
                }
            }
        }
		
		
	}
	}